// globals --- start

let globals = new Object();
globals.host = window.location.protocol + "//" + window.location.host + "/";    // http(https)://xxx.xxx.xxx/

// globals --- end


// ---------------------------------------------------
// ---------------------------------------------------


// graphs --- start

let graphs = new Object();
graphs.circle = new Object();
graphs.circle.green = "<div id = 'circle' style = 'background-color:#5BC648'></div>";
graphs.circle.red = "<div id = 'circle' style = 'background-color:#ff5858'></div>";
graphs.circle.index = "<div id = 'circle'></div>";


function error(message, state) {
    var innerData;
    switch (state) {
        case true:
            innerData = graphs.circle.green + "&nbsp&nbsp&nbsp&nbsp" + message;
            break;
        case false:
            innerData = graphs.circle.red + "&nbsp&nbsp&nbsp&nbsp" + message;
            break;
        default:
            innerData = graphs.circle.index + "&nbsp&nbsp&nbsp&nbsp" + message;
            break;
    };
    mdui.snackbar({
        message: innerData,
        position: 'right-top'
    });
    $(".mdui-snackbar").css("background-color", "#292929");
    $('.mdui-snackbar-text').css("color", "#AFAFAF")
};

// graphs --- end


// ---------------------------------------------------
// ---------------------------------------------------


// request --- start

let request = new Object();
request.ajax = new Object();

request.ajax.headers = [];  // headers请求头

["get", "post"].forEach(method => {
    request.ajax[method] = (url, params = {}) => {
        return $.ajax({
            url: url,
            type: method,
            data: params,
            headers: request.ajax.headers,
            success: function () {
                // 全局处理
            },
            error: function () {
                console.log('ajax异常')
                error('服务器错误', false);
            }
        });
    };
});

// request --- end


// ---------------------------------------------------
// ---------------------------------------------------


// pageApply --- start

let pageApply = new Object();
pageApply.index = new Object();
pageApply.index.card = new Object();

pageApply.index.card.mouldsUrl = globals.host + 'moulds/index.json';    // 获取模板json
pageApply.index.card.baseUrl = 'http://127.0.0.1:3000/api/';
pageApply.index.card.idToUrl = {    // id <-> Url 映射表
    'newTab': 'index/new',
    'hotTab': 'index/hot',
    'aboutMeTab': 'index/aboutMe'
};


// 获取对应ID数据
pageApply.index.card.getData = (url, params) => {
    return request.ajax.get(url, params).then((retJson) => { return retJson; });
};

// 更新主函数
pageApply.index.card.update = () => {
    // 1 获取HTML ID
    var callID = window.event.srcElement.id;






    // 2 通过ID获取对应数据

    // 2.1 查表确定API地址
    var url = pageApply.index.card.baseUrl + pageApply.index.card.idToUrl[callID];

    // 2.2 ajax请求数据
    pageApply.index.card.getData(url, {'lastID':'4', "device":'0'}).then((retJson) => {
        // 请求回调

        for (let i = 0; i < Object.keys(retJson).length; i++) {
            



            // 3 处理基础数据

            // 3.1 判断是否满足必须条件
            if (retJson[i]['mediaAddr'], retJson[i]['cardTitle'], retJson[i]['cardContent'] != undefined || '' || null) {

                // 3.2 获取模板数据
                request.ajax.get(pageApply.index.card.mouldsUrl).then((retMod) => {
                    var isAnonymous = false;

                    // 3.3 判断是否匿名卡片
                    if (retJson[i]['userName'] == undefined || '' || null) {
                        // 匿名 = True
                        isAnonymous = true;
                    };





                    // 4 将模板HTML替换为指定数据
                    var htmlData = retMod['index']['card']['template'];
                    var headImgHtml;

                    if (isAnonymous == true) {
                        headImgHtml = retMod['index']['card']['headImg_Anonymous'];
                        htmlData = htmlData.replace(/{{headImgHtml}}/g, headImgHtml);
                        htmlData = htmlData.replace(/{{userName}}/g, "Anonymous");
                        htmlData = htmlData.replace(/{{userSign}}/g, "Unknown");
                    }
                    else {
                        headImgHtml = retMod['index']['card']['headImg_UnAnonymous'].replace(/{{headImg}}/g, retJson[i]['headImg']);
                        htmlData = htmlData.replace(/{{headImgHtml}}/g, headImgHtml);
                        htmlData = htmlData.replace(/{{userName}}/g, retJson[i]['userName']);
                        htmlData = htmlData.replace(/{{userSign}}/g, retJson[i]['userSign']);
                    };
                    htmlData = htmlData.replace(/{{mediaAddr}}/g, retJson[i]['mediaAddr']);
                    htmlData = htmlData.replace(/{{cardTitle}}/g, retJson[i]['cardTitle']);
                    htmlData = htmlData.replace(/{{cardSubtitle}}/g, retJson[i]['cardSubtitle']);
                    htmlData = htmlData.replace(/{{cardContent}}/g, retJson[i]['cardContent']);





                    // 5 输出
                    document.getElementById(callID.replace(/Tab/g, '')).innerHTML += htmlData;



                }).catch(() => {
                    return false;
                });

            }
            else {
                // 必选项数据返回为空
                return false;
            };

        }
    }).catch(() => {
        return false;
    });

};


// pageApply --- end


// ---------------------------------------------------
// ---------------------------------------------------


// methods --- start


let methods = new Object();

methods.moulds = new Object();

methods.replace = (oldText, reg, newText) => {
    oldText.replace(reg, newText);
};


// methods --- end


// ---------------------------------------------------
// ---------------------------------------------------


// userMethod --- start


let userMethod = new Object();


// userMethod --- end